﻿/*
 * @author: wizardc
 */

using Dou.UI;
using TMPro;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;

namespace DouEditor.UI
{
    [CustomEditor(typeof(DToggle), true)]
    [CanEditMultipleObjects]
    public class DToggleEditor : SelectableEditor
    {
        [MenuItem("GameObject/DouUI/Toggle", false, 10)]
        private static void CreateUI(MenuCommand menuCommand)
        {
            var go = UIEditorUtil.CreateUIElement("Toggle");
            var image = go.AddComponent<DImage>();
            image.color = Color.white;
            image.type = Image.Type.Sliced;
            var button = go.AddComponent<DToggle>();
            button.targetGraphic = image;
            GameObject txtObj = UIEditorUtil.CreateGameObject("Text", go.transform);
            var text = txtObj.AddComponent<DText>();
            text.text = "Toggle";
            text.color = Color.black;
            text.alignment = TextAnchor.MiddleCenter;
            text.supportRichText = false;
            UIEditorUtil.PlaceUIElementRoot(go, menuCommand);
        }
        
        [MenuItem("GameObject/DouUI/Toggle - TextMeshPro", false, 10)]
        private static void CreateUI_TMP(MenuCommand menuCommand)
        {
            var go = UIEditorUtil.CreateUIElement("Toggle (TMP)");
            var image = go.AddComponent<DImage>();
            image.color = Color.white;
            image.type = Image.Type.Sliced;
            var button = go.AddComponent<DToggle>();
            button.targetGraphic = image;
            GameObject txtObj = UIEditorUtil.CreateGameObject("Text", go.transform);
            var text = txtObj.AddComponent<DTextMeshProUGUI>();
            text.text = "Toggle";
            text.color = Color.black;
            text.alignment = TextAlignmentOptions.Center;
            text.richText = false;
            UIEditorUtil.PlaceUIElementRoot(go, menuCommand);
        }
        
        private SerializedProperty _onValueChanged;
        
        protected override void OnEnable()
        {
            base.OnEnable();
            _onValueChanged = serializedObject.FindProperty("_onValueChanged");
        }
        
        public override void OnInspectorGUI()
        {
            var toggle = (DToggle) target;
            using (new GUILayout.VerticalScope())
            {
                toggle.isOn = EditorGUILayout.Toggle(new GUIContent("是否选中"), toggle.isOn);
                toggle.useGray = EditorGUILayout.Toggle(new GUIContent("置灰"), toggle.useGray);
            }
            base.OnInspectorGUI();
            serializedObject.Update();
            EditorGUILayout.PropertyField(_onValueChanged);
            serializedObject.ApplyModifiedProperties();
        }
    }
}
